﻿package hejie.admin.action;

import hejie.admin.dao.EthnicDao;
import hejie.admin.dao.RightDao;
import hejie.admin.service.EmployeeService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.opensymphony.xwork2.ActionSupport;


import publics.model.Employee;
import publics.util.MyUtils;
import publics.util.Page;


public class EmployeeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	EmployeeService employeeService;  //spring注入进来的 employeeService
	private RightDao rightDao;
	private EthnicDao ethnicDao ;
	
	private boolean success;   //此参数为必须的， extjs根据这个参数的值判断是否成功执行操作
	private Page page;   //这是分页查询用的辅助类，用此类可以解决分页查询中要返回多个参数的问题，推荐使用
	private String conditions;//查询条件（即搜索页面上文本框中输入的值），多个条件用空格分开
	private int start;   //分页查询的 要查询数据的开始号 ，和offset值相同
	private int limit;  // 每页多少个记录

	private Employee employee;
	private Integer id;
	private String rightId;
	private String ethnicId;
	
	public RightDao getRightDao() {
		return rightDao;
	}
	@Resource(name="rightDao")
	public void setRightDao(RightDao rightDao) {
		this.rightDao = rightDao;
	}
	public EthnicDao getEthnicDao() {
		return ethnicDao;
	}
	@Resource(name="ethnicDao")
	public void setEthnicDao(EthnicDao ethnicDao) {
		this.ethnicDao = ethnicDao;
	}
	public EmployeeService getEmployeeService() {
		return employeeService;
	}
	@Resource(name="employeeService")
	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
		System.out.println("EmployeeServiceImlp inject in EmployeeAction!");
	}

	
	
	
	/**
	 * 添加员工
	 * @return
	 */
	public String saveEmployee() {
		System.out.println("已经进入 Action 的 saveEmployee方法！");//测试用，可去掉
		employee.setCheckTime(new Date());
		employee.setRight(rightDao.findById(Integer.valueOf(this.getRightId())));
		employee.setEthnic(ethnicDao.findById(Integer.valueOf(this.getEthnicId())));
		id = (Integer) employeeService.saveEmployee(employee);
		if (id != null) {
			success = true;
		}else{
			success = false;  //失败返回
		}
		System.out.println("准备返回，success="+success);
		return SUCCESS;
	}

	/**
	 * 根据条件分页查找员工
	 * @return
	 */
	public String findAllEmployee() {  
		List<String> conditionList = new ArrayList<String>();   //模糊查询使用
		MyUtils.addToCollection(conditionList, MyUtils.split(conditions, " ,"));//模糊查询使用
		page = new Page(); // 实例化分页对象
		page.setConditions(conditionList);// 设置查询条件
		page.setStart(start);
		page.setLimit(limit = (limit == 0) ? 20 : limit);
		page = employeeService.findByPage(page);  //查询 
		if ((null != page.getRoot() && page.getTotalProperty() > 0 )||(null == page.getRoot() && page.getTotalProperty() == 0 )) {
			page.setSuccess(true);
		}else{
			page.setSuccess(false);//查找失败
		}
		return SUCCESS;
	}

	/**
	 * 删除员工
	 * 
	 * @return
	 */
	public String deleteEmployee() {
		
		if(Integer.valueOf(id) < 1){
			System.out.println("修改失败！员工id为="+employee.getId());
			success = false;
		}else{
			
			success = employeeService.deleteEmployee(Integer.valueOf(id));
		}
		return SUCCESS;
	}
	
	/**
	 * 修改员工指定字段的值
	 * @return
	 * @throws Exception
	 */
	public String updateEmployee() throws Exception {
		
		if(Integer.valueOf(employee.getId()) < 1){
			System.out.println("修改失败！ 员工id为="+employee.getId());
			success = false;
		}else{
			employee.setRight(rightDao.findById(Integer.valueOf(this.getRightId())));
			employee.setEthnic(ethnicDao.findById(Integer.valueOf(this.getEthnicId())));
			success = employeeService.updateEmployee(employee);
		}

		return SUCCESS;
	}
	
	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getConditions() {
		return conditions;
	}

	public void setConditions(String conditions) {
		this.conditions = conditions;
	}
	
	public String getRightId() {
		return rightId;
	}
	
	public void setRightId(String rightId) {
		this.rightId = rightId;
	}
	
	public String getEthnicId() {
		return ethnicId;
	}
	
	public void setEthnicId(String ethnicId) {
		this.ethnicId = ethnicId;
	}
}
